<?php
/**
 * User: Titan
 * Date: Sep 28, 2010
 * Time: 6:48:11 PM
 */

$load = 'install';

define('CONFIGURATION_MODE', 'INSTALL');

include_once('./includes/initialize.inc');
include_once('./includes/install.inc');
include_once('./includes/render.inc');
include_once('./includes/path.inc');

/*
 * The main installation function
 */
function install(&$variables)
{
	initialize_configuration($variables);
	
	if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form_id'] == 'install_configuration_form')
	{
		global $db_url;
		$db_url = 'mysql://' . $_POST['db_user'] . ':' . $_POST['db_pass'] . '@' . $_POST['db_host'] . ':' . $_POST['db_port'] . '/' . $_POST['db_path'];

		$form_values = array(
			'db_url' => $db_url
		);

		install_configuration_form_submit($form_values);
	}

	include_once('./includes/system.inc');
	include_once('./includes/form.inc');
	$verify = install_verify_settings();
	
	system_add_styles('assets/css/style.css');
	
	if ($verify)
	{
		require_once('./includes/database.inc');
		db_init();

		$installed = install_verify();
		if ($installed && $_GET['op'] != 'data')
		{
			// display some sort of confirmation or possibly an error?
			system_page_title('BugZapp Already Installed');
			install_output('<p style="font-weight:bold;">You have already installed BugZapp.</p>');
			
			return;
		}
	}
	
	if (!$verify)
	{
		system_page_title('Database Configuration');
		install_output(install_configuration_form());
		
		return;
	}

	if (empty($_GET['op']))
	{
		install_launch('install.php?op=config');
	}
	else if ($_GET['op'] == 'data')
	{
		initialize_system();
		
		install_development_data();
		install_launch('data?op=verifydev');
		
		return;
	}
    
	// install all of the database tables for the system
    initialize_system();
	$result = install_system();
	
	if ($result)
	{
		system_page_title('Successful Setup!');
		install_output('<p style="font-weight: bold;">Congratulations! You have setup the BugZapp database. <a href="' . $GLOBALS['base_url'] . '">Click here</a> to go to the BugZapp home page.</p><p><a href="install.php?op=data">Click here</a> to load development data into the database.</p>');
	}
	else
	{
		system_page_title('Setup Failed');
		install_output('<p style="font-weight: bold;">The BugZapp system failed to create its program database.</p>');
	}
}

function install_output($data = NULL)
{
	static $output = '';
	
	if (isset($data))
	{
		$output = $data;
	}
	
	return $output;
}

/*
 * Verification of the configured settings
 */
function install_verify_settings()
{
	global $db_url;

	if (!empty($db_url))
	{
		// TODO: validate db_url

		return TRUE;
	}

	return FALSE;
}

/*
 * Check to see if the site has been installed
 * 
 * NOTE: this function probably needs to be better
 */
function install_verify()
{
	$result = @db_query('SHOW TABLES');
	if ($result)
	{
		return mysql_num_rows($result) > 0;
	}
}

/*
 * returns the database configuration form
 */
function install_configuration_form()
{
	$form = array(
		'#id' => render_unique_id('install-configuration-form'),
		'#action' => 'install.php?op=init',
		'#method' => 'POST', 
		'#inputs' => array(
			'db_host' => array(
				'#type' => 'textfield',
				'#title' => 'Database Host',
				'#size' => 45,
				'#value' => 'localhost',
				'#description' => 'Enter the hostname of the server where your database is located.',
				'#order' => 0
			), 
			'db_path' => array(
				'#type' => 'textfield',
				'#title' => 'Database Name',
				'#size' => 45,
				'#value' => 'bugzapp',
				'#description' => 'Enter the name of your database. (If you have not previously setup a database, one will be created.)',
				'#order' => 1
			),
			'db_user' => array(
				'#type' => 'textfield',
				'#title' => 'Database Username',
				'#size' => 45,
				'#order' => 2
			), 
			'db_pass' => array(
				'#type' => 'password',
				'#title' => 'Database Password',
				'#size' => 45,
				'#order' => 3
			),
			'db_port' => array(
				'#type' => 'textfield',
				'#title' => 'Database Port',
				'#size' => 45,
				'#value' => 3306,
				'#description' => 'Only change this if your database server is located on a non-standard port.',
				'#order' => 4
			),
			'submit' => array(
				'#type' => 'submit',
				'#value' => 'Submit',
				'#order' => 5
			)
		)
	);
	
	$output = form_build($form, 'install_configuration_form');
	
	return $output;
}

/*
 * Handles the submission of the database configuration settings form
 */
function install_configuration_form_submit($form_result)
{
	$settings = array(
		'db_url' => $form_result['db_url']
	);
	
	install_write_settings($settings);
}

function install_development_data()
{
	include_once('./includes/development/development.inc');

	developer_data_create();
	feature_data_create();
	milestone_data_create();
	task_data_create();
}

function install_prerender_page(&$vars)
{
	$vars['content'] = install_output();
	$vars['tabs'] = NULL;
	$vars['menu'] = NULL;
}

$variables = array();
install($variables);

print(render('page', $variables));
